﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessComponents;
using BusinessComponents.Common;
using BusinessComponents.Managers;
using System.Data;

namespace WaterfrontLodge.WebForms.Shared
{
    public partial class RoomTypeView : System.Web.UI.Page
    {
        IEnumerable<RoomType> roomTypeList;
        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                roomTypeList = ManagerFactory.RoomTypeMgr.FindAllRoomTypes();
                grdSearchResult.DataSource = roomTypeList;
                grdSearchResult.DataBind();

                DataTable taskTable = BaseSorting.LinqToDataTable(roomTypeList);
                Session["TaskTable"] = taskTable;

            }

            if (Page.User.IsInRole("admin"))
            {
                grdSearchResult.Columns[0].Visible = true;
            }
            else
            {
                grdSearchResult.Columns[0].Visible = false;
            }
        }
        #endregion


        #region EventHaldlers

        protected void grdSearchResult_Sorting(object sender, GridViewSortEventArgs e)
        {
            //Retrieve the table from the session object.
            DataTable dt = Session["TaskTable"] as DataTable;

            if (dt != null)
            {

                //Sort the data.
                dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
                grdSearchResult.DataSource = Session["TaskTable"];
                grdSearchResult.DataBind();
            }


        }

        #endregion


        #region Functions

        private string GetSortDirection(string column)
        {

            // By default, set the sort direction to ascending.
            string sortDirection = "ASC";

            // Retrieve the last column that was sorted.
            string sortExpression = ViewState["SortExpression"] as string;

            if (sortExpression != null)
            {
                // Check if the same column is being sorted.
                // Otherwise, the default value can be returned.
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }

            // Save new values in ViewState.
            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;

            return sortDirection;
        }

        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/WebForms/RoomType/RoomTypeAdd1.aspx");
        }

        protected void grdSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image imgPicture = (Image)e.Row.FindControl("imgPicture");
                if (imgPicture.ImageUrl == null || imgPicture.ImageUrl == string.Empty)
                    imgPicture.ImageUrl = "~/Image/EmptyRoom.jpg";
            }

        }


    }
}